package org.jeecg.modules.demo.mall.yxshopproduct.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.demo.api.UpmsHelloApi;
import org.jeecg.modules.demo.entity.Dept;
import org.jeecg.modules.demo.mall.entity.YxShopProduct;
import org.jeecg.modules.demo.mall.entity.YxShopSku;
import org.jeecg.modules.demo.mall.entity.YxStoreCategory;
import org.jeecg.modules.demo.mall.yxshopproduct.service.IYxShopProductService;
import org.jeecg.modules.demo.mall.yxshopsku.service.IYxShopSkuService;
import org.jeecg.modules.demo.mall.yxstorecategory.service.IYxStoreCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: yx_shop_product
 * @Author: jeecg-boot
 * @Date: 2023-10-23
 * @Version: V1.0
 */
@Api(tags = "yx_shop_product")
@RestController
@RequestMapping("/mall/yxshopproduct/yxShopProduct")
@Slf4j
public class YxShopProductController extends JeecgController<YxShopProduct, IYxShopProductService> {
    @Autowired
    private IYxShopProductService yxShopProductService;
    @Autowired
    private IYxStoreCategoryService yxStoreCategoryService;
    @Autowired
    private UpmsHelloApi upmsHelloApi;

    @Autowired
    private IYxShopSkuService yxShopSkuService;

    /**
     * 分页列表查询
     *
     * @param yxShopProduct
     * @param pageNo
     * @param pageSize
     * @return
     */
    //@AutoLog(value = "yx_shop_product-分页列表查询")
    @ApiOperation(value = "yx_shop_product-分页列表查询", notes = "yx_shop_product-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<YxShopProduct>> queryPageList(YxShopProduct yxShopProduct,
                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                      String merId) {
        QueryWrapper<YxShopProduct> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(merId)) {
            queryWrapper.eq("mer_id", merId);
        }
        if (StringUtils.isNotBlank(yxShopProduct.getStoreName())) {
            queryWrapper.like("store_name", yxShopProduct.getStoreName());
        }
        if (yxShopProduct.getIsShow() != null) {
            queryWrapper.eq("is_show", yxShopProduct.getIsShow());
        }
        if (yxShopProduct.getExamineStatus() != null) {
            queryWrapper.eq("examine_status", yxShopProduct.getExamineStatus());
        }
        queryWrapper.orderByDesc("create_time");
        Page<YxShopProduct> page = new Page<YxShopProduct>(pageNo, pageSize);
        IPage<YxShopProduct> pageList = yxShopProductService.page(page, queryWrapper);
        if (CollectionUtils.isEmpty(pageList.getRecords())) {
            return Result.ok(pageList);
        }
        pageList.getRecords().forEach(yxShopProduct1 -> {
            YxStoreCategory categoryServiceById = yxStoreCategoryService.getById(Integer.valueOf(yxShopProduct1.getCateId()));
            if (categoryServiceById != null) {
                yxShopProduct1.setCateName(categoryServiceById.getCateName());
            }
            Dept dept = upmsHelloApi.queryByDept(Long.valueOf(yxShopProduct1.getMerId()));
            if (dept != null) {
                yxShopProduct1.setMerName(dept.getName());
            }
            yxShopProduct1.setSkus(yxShopSkuService.querySkuList(yxShopProduct1.getId()));
        });
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param yxShopProduct
     * @return
     */
    @AutoLog(value = "yx_shop_product-添加")
    @ApiOperation(value = "yx_shop_product-添加", notes = "yx_shop_product-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody YxShopProduct yxShopProduct) {
        yxShopProduct.setExamineStatus(1);
        yxShopProductService.save(yxShopProduct);
        if (!CollectionUtils.isEmpty(yxShopProduct.getSkus())) {
            yxShopProduct.getSkus().forEach(yxShopSku -> {
                yxShopSku.setMerId(yxShopProduct.getMerId());
                yxShopSku.setMerName(yxShopProduct.getMerName());
                yxShopSku.setStoreId(yxShopProduct.getId());
                yxShopSku.setStoreName(yxShopProduct.getStoreName());
                yxShopSkuService.save(yxShopSku);
            });
        }
        return Result.OK("添加成功！");
    }


    /**
     * 编辑
     *
     * @param yxShopProduct
     * @return
     */
    @AutoLog(value = "yx_shop_product-编辑")
    @ApiOperation(value = "yx_shop_product-编辑", notes = "yx_shop_product-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody YxShopProduct yxShopProduct) {
        yxShopProductService.updateById(yxShopProduct);
        //规格修改
        if (!CollectionUtils.isEmpty(yxShopProduct.getSkus())) {
            yxShopProduct.getSkus().forEach(yxShopSku -> {
                yxShopSkuService.updateById(yxShopSku);
            });
        }
        return Result.OK("编辑成功!");
    }


    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "yx_shop_product-通过id删除")
    @ApiOperation(value = "yx_shop_product-通过id删除", notes = "yx_shop_product-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) Long id) {
        yxShopProductService.removeById(id);
        List<YxShopSku> yxShopSkus = yxShopSkuService.querySkuList(id);
        //如果存在规格  则进行删除
        if (!CollectionUtils.isEmpty(yxShopSkus)) {
            yxShopSkus.forEach(yxShopSku -> {
                yxShopSkuService.removeById(yxShopSku.getId());
            });
        }
        return Result.OK("删除成功!");
    }


    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "yx_shop_product-批量删除")
    @ApiOperation(value = "yx_shop_product-批量删除", notes = "yx_shop_product-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.yxShopProductService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }


    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "yx_shop_product-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<YxShopProduct> queryById(@RequestParam(name = "id", required = true) Long id) {
        YxShopProduct yxShopProduct = yxShopProductService.getById(id);
        if (yxShopProduct == null) {
            return Result.error("未找到对应数据");
        }
        YxStoreCategory categoryServiceById = yxStoreCategoryService.getById(Integer.valueOf(yxShopProduct.getCateId()));
        if (categoryServiceById != null) {
            yxShopProduct.setCateName(categoryServiceById.getCateName());
        }
        Dept dept = upmsHelloApi.queryByDept(Long.valueOf(yxShopProduct.getMerId()));
        if (dept != null) {
            yxShopProduct.setMerName(dept.getName());
        }
        yxShopProduct.setSkus(yxShopSkuService.querySkuList(yxShopProduct.getId()));
        return Result.OK(yxShopProduct);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param yxShopProduct
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, YxShopProduct yxShopProduct) {
        return super.exportXls(request, yxShopProduct, YxShopProduct.class, "yx_shop_product");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, YxShopProduct.class);
    }


    /**
     * 审核/批量审核
     *
     * @param ids
     * @return
     */
//    @AutoLog(value = "审核/批量审核")
    @ApiOperation(value = "审核/批量审核", notes = "审核/批量审核")
    @GetMapping(value = "/updateExamineStatus")
    public Result<String> updateExamineStatus(@RequestParam String ids, @RequestParam Integer examineStatus) {
        List<String> productIds = Arrays.asList(ids.split(","));
        if (CollectionUtils.isEmpty(productIds)) {
            return Result.OK("请选择要审核的商品!");
        }
        List<Long> longList = productIds.stream().map(s -> {
            Long aLong = Long.valueOf(s);
            return aLong;
        }).collect(Collectors.toList());
        QueryWrapper<YxShopProduct> yxShopProductUpdateWrapper = new QueryWrapper<>();
        yxShopProductUpdateWrapper.in("id", longList);
        List<YxShopProduct> yxShopProductList = this.yxShopProductService.list(yxShopProductUpdateWrapper);
        if (CollectionUtils.isEmpty(yxShopProductList)) {
            return Result.OK("没有要修改的数据!");
        }
        //把数据都修改成审核成功
        yxShopProductList.forEach(yxShopProduct -> {
            yxShopProduct.setExamineStatus(examineStatus);
        });
        this.yxShopProductService.updateBatchById(yxShopProductList);
        return Result.OK("批量审核成功!");
    }

}
